/**
 * 
 */
package com.nari.heatshare.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.IBuildingDao;
import com.nari.baseinfo.dao.ICommunityDao;
import com.nari.baseinfo.dao.IConcentratorDao;
import com.nari.baseinfo.dao.IHeatRoomDao;
import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.IConcentrator;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.heatshare.dao.ITemperaturePointDao;
import com.nari.heatshare.model.ITemperaturePoint;
import com.nari.heatshare.utils.excelupload.TemperaturePointParser;
import com.nari.heatshare.utils.excelupload.parseentity.ExcelTempPointEntity;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-11
 */
public class TemperaturePointServiceImpl extends GenericServiceImpl<ITemperaturePoint, String>
		implements ITemperaturePointService {
	private IConcentratorDao iConcentratorDao;
	private ICommunityDao iCommunityDao;
	private IBuildingDao iBuildingDao;
	private IHeatRoomDao iHeatRoomDao;
	
	/**
	 * 分页查询温度点列表
	 * @param seriousNum 温度点编号(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param concentratorUID 所属集中器(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public ResultWithPageCond<ITemperaturePoint> queryTempPointPage(String seriousNum, 
			String projectUID, String communityUID, String buildingUID, String concentratorUID, 
			PageCond pageCond) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		return iTemperaturePointDao.queryTempPointPage(seriousNum, projectUID, communityUID, buildingUID, concentratorUID, pageCond);
	}
	
	/**
	 * 通过温度点列表查询与之关联的小区ID-小区名称映射表
	 * @param tempPointList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public Map<String, String> queryReferCommunitysByTpList(List<ITemperaturePoint> tempPointList) 
			throws Exception {
		List<String> communityIDList = null;
		if(tempPointList!=null && tempPointList.size()>0) {
			communityIDList = new ArrayList<String>();
			for(ITemperaturePoint temperaturePoint: tempPointList) {
				communityIDList.add(temperaturePoint.getCommunityID());
			}
		}
		return iCommunityDao.queryIdNameMapByIDList(communityIDList);
	}
	
	/**
	 * 通过温度点列表查询与之关联的楼栋ID-楼栋名称映射表
	 * @param tempPointList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public Map<String, String> queryReferBuildingsByTpList(List<ITemperaturePoint> tempPointList) 
			throws Exception {
		List<String> buildingIDList = null;
		if(tempPointList!=null && tempPointList.size()>0) {
			buildingIDList = new ArrayList<String>();
			for(ITemperaturePoint temperaturePoint: tempPointList) {
				buildingIDList.add(temperaturePoint.getBuildingID());
			}
		}
		return iBuildingDao.queryIdNameMapByIDList(buildingIDList);
	}
	
	/**
	 * 通过温度点列表查询与之关联的热用户ID-门牌号映射表
	 * @param tempPointList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public Map<String, String> queryReferRoomsByTpList(List<ITemperaturePoint> tempPointList) 
			throws Exception {
		List<String> roomIDList = null;
		if(tempPointList!=null && tempPointList.size()>0) {
			roomIDList = new ArrayList<String>();
			for(ITemperaturePoint temperaturePoint: tempPointList) {
				roomIDList.add(temperaturePoint.getRoomId());
			}
		}
		return iHeatRoomDao.queryIdNameMapByIDList(roomIDList);
	}
	
	/**
	 * 新增或修改温度点信息
	 * 说明：同一集中器下温度点编号不可以重复
	 * @param temperaturePoint
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public ResponseMsg saveOrUpdateTempPoint(ITemperaturePoint temperaturePoint) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		//查询温度点所属的集中器
		IConcentrator concentrator = iConcentratorDao.queryEntityById(temperaturePoint.getConcentratorID());
		//统计同一集中器下的指定温度点的个数
		int counter = iTemperaturePointDao.countTempPointNumByConcentrator(temperaturePoint.getId(), 
				temperaturePoint.getConcentratorID(), temperaturePoint.getSeriousNum());
		if(counter > 0) {
			return ResponseMsg.getInstance(false, concentrator.getConcentratorNo()+"集中器下已存在编号为【"+temperaturePoint.getSeriousNum()+"】的温度点，请重新录入温度点编号！");
		}
		//设置温度点所属的集中器编号
		temperaturePoint.setConcentratorNo(concentrator.getConcentratorNo());
		//设置温度点所属的门牌号
		if(temperaturePoint.getHouseNo()!=null && !"".equals(temperaturePoint.getHouseNo().trim())) {
			IHeatRoom heatRoom = iHeatRoomDao.queryRoomInfoByhouseNoBuildingUID(temperaturePoint.getHouseNo(), temperaturePoint.getBuildingID());
			if(heatRoom != null) {
				temperaturePoint.setRoomId(heatRoom.getId());
			}
		} else if(temperaturePoint.getRoomId()!=null && !"".equals(temperaturePoint.getRoomId())) {
			IHeatRoom heatRoom = iHeatRoomDao.queryEntityById(temperaturePoint.getRoomId());
			temperaturePoint.setHouseNo(heatRoom.getHouseNo());
		}
		//温度点信息入库
		if(temperaturePoint.getId()!=null && !"".equals(temperaturePoint.getId())) {
			this.getiGenericDao().updateEntity(temperaturePoint, temperaturePoint.getId());
		} else {
			this.getiGenericDao().saveEntity(temperaturePoint);
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 通过集中器所属的温度点编号查询温度点信息
	 * 说明：原则上，同一集中器下温度点编号唯一
	 * @param concentratorNo
	 * @param seriousNum
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-13
	 */
	public ITemperaturePoint queryTempPointBySeriousNum(String concentratorNo, 
			String seriousNum) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		return iTemperaturePointDao.queryTempPointBySeriousNum(concentratorNo, seriousNum);
	}
	
	/**
	 * 温度点信息资料批量上传
	 * @param excelFile
	 * @param excelFileName
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-13
	 */
	public ResponseMsg saveOrUpdateTempPointBatch(File excelFile, 
			String excelFileName) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		//Excel文件解析
		TemperaturePointParser excelParser = TemperaturePointParser.getInstance();
		ResponseMsg rmsg = excelParser.parseTempPointExcel(excelFile, excelFileName);
		if(rmsg.isSuccess()) {
			//读取Excel解析信息
			List<ExcelTempPointEntity> tempPointList = excelParser.getExcelParseData();
			for(Iterator<ExcelTempPointEntity> it=tempPointList.iterator(); it.hasNext();) {
				ExcelTempPointEntity tempPointEntity = it.next();
				//查询温度点对应的楼栋信息
				IBuilding building = iBuildingDao.queryBuildingInfoByBuildingID(tempPointEntity.getBuildingID());
				if(building == null) {
					String msg = "缺少建筑物编号为【"
							+tempPointEntity.getBuildingID()
							+"】的楼栋信息！";
					return ResponseMsg.getInstance(false, msg);
				}
				//查询温度点信息
				ITemperaturePoint temperaturePoint = iTemperaturePointDao
						.queryTempPointBySeriousNum(tempPointEntity.getConcentratorNo(), tempPointEntity.getSeriousNum());
				if(temperaturePoint == null) {
					temperaturePoint = new ITemperaturePoint();
				}
				//设置温度点所属关系
				temperaturePoint.setBuildingID(building.getId());
				temperaturePoint.setCommunityID(building.getCommunityID());
				temperaturePoint.setProjectID(building.getProjectID());
				//查询温度点对应的集中器信息
				IConcentrator concentrator = iConcentratorDao.queryByConcentratorNo(tempPointEntity.getConcentratorNo());
				if(concentrator == null) {
					concentrator = new IConcentrator();
					//所属项目
					concentrator.setProjectID(building.getProjectID());
					//所属小区
					concentrator.setCommunityID(building.getCommunityID());
					//所属楼栋
					concentrator.setBuildingID(building.getId());
					//集中器号
					concentrator.setConcentratorNo(tempPointEntity.getConcentratorNo());
					iConcentratorDao.saveEntity(concentrator);
				}
				//设置温度点对应的集中器信息
				temperaturePoint.setConcentratorID(concentrator.getId());
				temperaturePoint.setConcentratorNo(concentrator.getConcentratorNo());
				//设置温度点对应的热用户信息
				IHeatRoom heatRoom = iHeatRoomDao.queryRoomInfoByhouseNoBuildingUID(tempPointEntity.getHouseNo(), building.getId());
				if(heatRoom != null) {
					temperaturePoint.setRoomId(heatRoom.getId());
				}
				temperaturePoint.setHouseNo(tempPointEntity.getHouseNo());
				//温度点编号
				temperaturePoint.setSeriousNum(tempPointEntity.getSeriousNum());
				//备注
				temperaturePoint.setRemark(tempPointEntity.getRemark());
				this.getiGenericDao().saveOrUpdateEntity(temperaturePoint);
			}
			return ResponseMsg.getInstance(true, "");
		}
		return rmsg;
	}
	
	/**
	 * 通过温度点所属关系统计温度点的个数
	 * @param projectUID
	 * @param communityUID
	 * @param buildingUID
	 * @param concentratorID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public int countTempPointSumByBelongs(String projectUID, String communityUID, 
			String buildingUID, String concentratorUID) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		return iTemperaturePointDao.countTempPointSumByBelongs(projectUID, communityUID, buildingUID, concentratorUID);
	}
	
	/**
	 * 通过集中器唯一标识查询对应的温度点列表
	 * @param concentratorUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public List<ITemperaturePoint> queryTempPointsByConcentratorUID(String concentratorUID) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		return iTemperaturePointDao.queryTempPointsByConcentratorUID(concentratorUID);
	}
	
	/**
	 * 通过温度点所属关系查询(所属项目、所属小区、所属楼栋、集中器)查询温度点列表
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param concentratorUID 所属集中器(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-25
	 */
	public List<ITemperaturePoint> queryTempPointListByBelongs(String projectUID, 
			String communityUID, String buildingUID, String concentratorUID) throws Exception {
		ITemperaturePointDao iTemperaturePointDao = (ITemperaturePointDao) this.getiGenericDao();
		return iTemperaturePointDao.queryTempPointListByBelongs(projectUID, communityUID, buildingUID, concentratorUID);
	}

	/**
	 * @return the iConcentratorDao
	 */
	public IConcentratorDao getiConcentratorDao() {
		return iConcentratorDao;
	}

	/**
	 * @param iConcentratorDao the iConcentratorDao to set
	 */
	public void setiConcentratorDao(IConcentratorDao iConcentratorDao) {
		this.iConcentratorDao = iConcentratorDao;
	}

	/**
	 * @return the iCommunityDao
	 */
	public ICommunityDao getiCommunityDao() {
		return iCommunityDao;
	}

	/**
	 * @param iCommunityDao the iCommunityDao to set
	 */
	public void setiCommunityDao(ICommunityDao iCommunityDao) {
		this.iCommunityDao = iCommunityDao;
	}

	/**
	 * @return the iBuildingDao
	 */
	public IBuildingDao getiBuildingDao() {
		return iBuildingDao;
	}

	/**
	 * @param iBuildingDao the iBuildingDao to set
	 */
	public void setiBuildingDao(IBuildingDao iBuildingDao) {
		this.iBuildingDao = iBuildingDao;
	}

	/**
	 * @return the iHeatRoomDao
	 */
	public IHeatRoomDao getiHeatRoomDao() {
		return iHeatRoomDao;
	}

	/**
	 * @param iHeatRoomDao the iHeatRoomDao to set
	 */
	public void setiHeatRoomDao(IHeatRoomDao iHeatRoomDao) {
		this.iHeatRoomDao = iHeatRoomDao;
	}
	
}
